//
//  CoreDataViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/12/14.
//  Copyright © 2015年 ltz. All rights reserved.
//

#import "CoreDataViewController.h"
#import "PersonTools.h"
#import "Person.h"
#import "IdCard.h"
@interface CoreDataViewController ()<UISearchBarDelegate>
{
    UISearchBar *  _searchBar;
    PersonTools *  pTools;
}
@property (nonatomic, strong) NSMutableArray *persons;

@end

@implementation CoreDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xfffffff];
    [self showNavigationRightButton:@"添加" tintColor:0xfffffff];
    _searchBar = [[UISearchBar alloc] init];
    _searchBar.delegate = self;
    self.navigationItem.titleView = _searchBar;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [self.view addGestureRecognizer:singleTap];
    
      pTools=[PersonTools shareInstanceWithStoreName:nil];
    
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}






-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    
    [_searchBar resignFirstResponder];
}
-(void)navigationRightButtonClick:(id)sender{
    
    
    NSArray *nameArr = @[@"gogoing",@"duang",@"uzi",@"omg"];
    
    NSString *name = [NSString stringWithFormat:@"%@%d",nameArr[arc4random_uniform(4)],arc4random_uniform(200)];
    NSString *phone = [NSString stringWithFormat:@"%d",arc4random_uniform(10000)+10000];
    Person *p = [[Person alloc] init];
    p.name=name;
    p.age=@"19";
    IdCard *card=[[IdCard alloc] init];
    card.no=phone;
    p.card=card;
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.persons.count inSection:0];
    [self.persons addObject:p];
    
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    
    [pTools saveWithPerson:p];
   
    

}

-(NSMutableArray *)persons{
    
    if(_persons==nil){
        
        _persons=[pTools persons];

        if (_persons == nil) {
            _persons = [NSMutableArray array];
        }
    }
    
    return _persons;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSLog(@"self.persons.count %lu",(unsigned long)self.persons.count);
    return self.persons.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseId = @"ContractWorkTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    Person * p= self.persons[indexPath.row];
    cell.textLabel.text=p.name;

    return cell;
    
    
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    _persons=[pTools getSearchPersons:searchText];
    
    [self.tableView reloadData];
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    // 给服务器发送请求
    
    // 刷新表格
    
    NSLog(@"%s",__func__);
}


#pragma mark  数据源方法 提交编辑操作时调用 自动实现左划删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(editingStyle ==UITableViewCellEditingStyleDelete){
        
        
        Person *p= _persons[indexPath.row];
        if ([pTools deletePerson:p]) {
            [_persons removeObjectAtIndex:indexPath.row];
            //刷新所有数据
            //    [_tableView reloadData ];
            //有动画的删除
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
        

        
    }
    
    
}

//修改 左划删除 按钮
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath{
    
    return @"删除";
    
}


@end
